<template>
  <el-dialog class="form" title="新建文件夹" :visible.sync="dialogFormVisible">
    <el-form :model="form" label-width="100px" label-position="left" @submit.native="submit">
      <el-form-item label="文件夹名称">
        <el-input v-model="form.name" auto-complete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<style lang="scss" rel="stylesheet/scss">
  .form {
    .el-dialog {
      min-width: 300px;
    }
  }

</style>

<script type="text/babel">
  export default {
    props: {
      path: String
    },
    data() {
      return {
        dialogFormVisible: false,
        form: {}
      }
    },
    watch: {},
    computed: {},
    methods: {
      openDialog () {
        this.dialogFormVisible = true
        this.form.name = ''
      },
      submit () {
        let reg = /[\\/:*?"<>|]+/

        this.form.name = this.form.name.trim()
        if (!this.form.name) {
          this.$message.error('请输入文件夹名称！');
          return
        }
        if (reg.test(this.form.name)) {
          this.$message.error('存在非法字符  \\/:*?"<>|');
          return
        }

        try {
          this.$fs.mkdirSync(this.path + this.form.name)
          this.dialogFormVisible = false
          this.$emit('submit', this.form.name)

          this.$message({
            message: '文件夹创建成功!',
            type: 'success',
            duration: 1000,
          });

        } catch (err) {
          this.$message.error('文件夹创建失败！');
        }
      }
    },
    mounted() {
    },
  }
</script>

